﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Frame;

namespace ApplicationManagement
{
    public partial class Property : Form
    {
        private App app = null;
        private AppClass appClass = null;

        private Global m_global;
        public Global global
        {
            set { m_global = value; }
            get { return m_global; }
        }

        public Property(App app)
        {
            InitializeComponent();
            this.app = app;
            ScreenHelper.ShowScreen(this);
        }
        public Property(AppClass appClass)
        {
            InitializeComponent();
            this.appClass = appClass;
            ScreenHelper.ShowScreen(this);
        }


        private void btnPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            if (IsApp())
            {
                of.DefaultExt = "exe";
                of.Filter = "应用程序 (*.exe)|*.exe";
                lPath.Text = "路径";               
            }
            else
            {
                of.DefaultExt = "png;ico";
                of.Filter = "图形(*.ico, *.png)|*.ico;*.png";
                lPath.Text = "图标";
            }
            string iniPath = Path.GetDirectoryName(tbxPath.Text);
            if (Directory.Exists(iniPath))
                of.InitialDirectory = iniPath;
            if (of.ShowDialog() == DialogResult.OK)
            {
                if (IsApp())
                {
                    if (tbxName.Text == string.Empty)
                        tbxName.Text = System.IO.Path.GetFileNameWithoutExtension(of.FileName);
                    tbxPath.Text = of.FileName;
                    this.Icon = Icon.ExtractAssociatedIcon(tbxPath.Text);
                }
                else
                {
                    tbxPath.Text = of.FileName;
                    this.Icon = Icon.ExtractAssociatedIcon(tbxPath.Text);
                }
            }
        }

        private void Property_Load(object sender, EventArgs e)
        {
            if(!IsApp())
            {
                tbxName.Text = appClass.Name;
                if (File.Exists(global.get_full_address(appClass.image)))
                {
                    this.Icon = Icon.ExtractAssociatedIcon(global.get_full_address(appClass.image));
                    tbxPath.Text = global.get_full_address(appClass.image);
                }
            }
            else
            {
                tbxName.Text = app.Name;
                tbxPath.Text = app.Path;
                if (File.Exists(app.Path))
                    this.Icon = Icon.ExtractAssociatedIcon(app.Path);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (IsApp())
            {
                app.Name = tbxName.Text;
                app.Path = tbxPath.Text;
            }
            else if (appClass == null)
            {
                appClass.Name = tbxName.Text;
                appClass.image = m_global.get_file_name(tbxPath.Text);
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private bool IsApp()
        {
            return app != null;
        }
    }
}
